const path = require("path");
const async = require("async");

// const getNewsList = require("./getNewsList");
const getNewsList = require("./getNewsListWithApi"); // api方式更直接
const getHtmlFromUrl = require("./getHtmlFromUrl");

const SOURCE = "https://www.huxiu.com/article/";

function getHtml(item, cb) {
  getHtmlFromUrl(item.url)
    .then((article) => cb(null, article))
    .catch((error) => {
      console.log("FAILDED ON: " + item.url);
      cb(null, {
        title: null,
        data: null,
      });
    });
}

// 导出的是一个用于asyn的瀑布流处理数组
module.exports = [
  (cb) => {
    getNewsList(SOURCE)
      .then((list) => cb(null, list))
      .catch((error) => cb(error));
  },
  (list, cb) => {
    console.log(list.map((d) => d.title).join("\n"));
    async.mapLimit(list, 5, getHtml, (error, results) => {
      if (error) return cb(error);
      cb(null, results);
    });
  },
  (list, cb) => {
    list = list.filter((d) => d.data && d.title);

    let option = {
      title: "虎嗅网",
      author: "1cr",
      version: 3,
      cover: path.resolve(__dirname, "cover.jpg"),
      appendChapterTitles: false,
      content: list,
    };
    let d = new Date(Date.now()),
      date = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate(),
      name = date + "-HuXiu.epub";

    cb(null, { option, name });
  },
];
